/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jxsm.cz.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jxsm.cz.biz.BizBannerInfo;
import com.jxsm.cz.model.BannerInfo;
import com.jxsm.jxbrms.query.QueryController;
import com.jxsm.config.Message;
import com.jxsm.web.upload.JUploadFileInfo;
import com.jxsm.web.upload.JUploadInfo;
import java.util.List;

/**
 *
 * @author lenovo
 */
public class BannerInfoController extends QueryController {

    @Override
    public void adding() {
        super.adding();
    }

    public void added() {
        //获取上传的banner图片
        JUploadInfo jui = this.getUploadInfo();
        List<JUploadFileInfo> uploadFiles = jui.getUploadFiles();
        BizBannerInfo bizBannerInfo = new BizBannerInfo();
        BannerInfo bi = this.getModel(BannerInfo.class);
        String inputName = null;
        String fileName = null;
        JSONArray jsonArray = new JSONArray();
        if (!uploadFiles.isEmpty()) {
            for (JUploadFileInfo uf : uploadFiles) {
                JSONObject json = new JSONObject();
                logger.debug("filename:{}  pname:{}  oldname:{} ", uf.getFileName(),
                        uf.getFileInputName(), uf.getFileOldName());
                inputName = uf.getFileInputName();
                fileName = uf.getFileName();
                json.put("fileName", inputName);
                json.put("filePath", fileName);
                jsonArray.add(json);
            }
            bi.set("image", jsonArray.toJSONString());
        }
        bizBannerInfo.addTx(bi);
        if (bizBannerInfo.isSuccess()) {
            this.renderAddSuccess(bi.getStr("title"));
        } else {
            this.renderAddFail(bizBannerInfo.getErrorInfo(), bi.getStr("title"));
        }
    }

    public void edited() {
        BizBannerInfo bizBannerInfo = new BizBannerInfo();
        JUploadInfo jui = this.getUploadInfo();
        List<JUploadFileInfo> uploadFiles = jui.getUploadFiles();
        BannerInfo bi = this.getModel(BannerInfo.class);
        logger.debug("uploadFilesSize {}", uploadFiles.size());
        
        //如果当前没有上传文件,则不修改image字段,如果存在上传文件,直接覆盖原本的image
        if (uploadFiles.size() != 0) {
            JSONArray jsonArray = new JSONArray();
            String inputName = null;
            String fileName = null;
            for (int i = 0; i < uploadFiles.size(); i++) {
                JUploadFileInfo uf = uploadFiles.get(i);
                JSONObject json = new JSONObject();
                inputName = uf.getFileInputName();
                fileName = uf.getFileName();
                json.put("fileName", inputName);
                json.put("filePath", fileName);
                jsonArray.add(json);
            }
            bi.set("image", jsonArray.toJSONString());
        }

        bizBannerInfo.editTx(bi);
        if (bizBannerInfo.isSuccess()) {
            this.setMessage(new Message(0, "0", "信息更新成功"));
            this.renderMessage();
        } else {
            this.setMessage(new Message(2, "0", "信息更新失败!"));
            this.renderMessage();
        }
    }

    @Override
    public void editing() {
        String id = this.getPara(0);
        BannerInfo bi = BannerInfo.dao.findById(id);
        this.setAttr("bannerInfo", bi);
        super.editing();
    }

    public void delete() {
        BizBannerInfo bizBannerInfo = new BizBannerInfo();
        bizBannerInfo.deleteTx(this.getPara(0));
        if (bizBannerInfo.isSuccess()) {
            this.setMessage(new Message(0, "0", "删除成功"));
            this.renderMessage();
        } else {
            this.setMessage(new Message(2, "0", "删除失败!"));
            this.renderMessage();
        }
    }
}
